package com.leo.train.business.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leo.train.business.dto.CarriageQueryDto;
import com.leo.train.business.entity.Carriage;
import com.leo.train.business.mapper.CarriageMapper;
import com.leo.train.business.service.CarriageService;
import com.leo.train.business.vo.CarriageQueryVo;
import com.leo.train.common.enums.SeatColEnum;
import com.leo.train.common.enums.SeatTypeEnum;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.resp.PageResult;
import com.leo.train.common.util.SnowUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarriageServiceImpl implements CarriageService {
    @Autowired
    private CarriageMapper carriageMapper;

    @Override
    public void save(Carriage carriage) {
        Long id = carriage.getId();
        if(id == null) {
            // 校验
            Carriage carriageExist = selectByTrainCodeAndIndex(carriage.getTrainCode(), carriage.getIndex());
            if(carriageExist != null) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
            }
            // 新增
            carriage.setId(SnowUtil.getSnowflakeNextId());
            carriage.setCreateTime(new Date());
            carriage.setUpdateTime(new Date());
            // 根据座位类型获取列数
            int colCount = SeatColEnum.getColsByType(carriage.getSeatType()).size();
            carriage.setColCount(colCount);
            // 计算出座位数
            int seatCount = colCount * carriage.getRowCount();
            carriage.setSeatCount(seatCount);
            carriageMapper.insert(carriage);
        } else {
            // 编辑
            // 根据座位类型获取列数
            int colCount = SeatColEnum.getColsByType(carriage.getSeatType()).size();
            carriage.setColCount(colCount);
            // 计算出座位数
            int seatCount = colCount * carriage.getRowCount();
            carriage.setSeatCount(seatCount);
            carriage.setUpdateTime(new Date());
            carriageMapper.update(carriage);
        }

    }

    @Override
    public void deleteById(Long id) {
        carriageMapper.deleteById(id);
    }

    @Override
    public PageResult<CarriageQueryVo> list(CarriageQueryDto carriageQueryDto) {
        PageHelper.startPage(carriageQueryDto.getPage(), carriageQueryDto.getPageSize());
        Page<Carriage> page = carriageMapper.list(carriageQueryDto);
        List<CarriageQueryVo> carriageQueryVoList = page.getResult().stream().map(carriage -> {
            CarriageQueryVo carriageQueryVo = new CarriageQueryVo();
            BeanUtils.copyProperties(carriage, carriageQueryVo);
            carriageQueryVo.setSeatTypeName(SeatTypeEnum.getEnumByCode(carriage.getSeatType()).getDesc());
            return carriageQueryVo;
        }).collect(Collectors.toList());
        return new PageResult<>(page.getTotal(), carriageQueryVoList);
    }

    @Override
    public CarriageQueryVo detail(Long id) {
        Carriage carriage = carriageMapper.selectById(id);
        if(carriage == null) {
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_CARRIAGE_NOT_EXIST);
        }
        CarriageQueryVo carriageQueryVo = new CarriageQueryVo();
        BeanUtils.copyProperties(carriage, carriageQueryVo);
        carriageQueryVo.setSeatTypeName(SeatTypeEnum.getEnumByCode(carriage.getSeatType()).getDesc());
        return carriageQueryVo;
    }

    /**
     * 定义一个唯一键方法
     */
    public Carriage selectByTrainCodeAndIndex(String trainCode,Integer index) {
        Carriage carriage = carriageMapper.selectByTrainCodeAndIndex(trainCode, index);
        if (carriage != null) {
            return carriage;
        }
        return null;
    }
}
